package com.turing.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turing.common.util.StringUtils;
import com.turing.user.dao.RoleFunctionPrivilegeMapper;
import com.turing.user.entity.FunctionPrivilege;
import com.turing.user.entity.RoleFunctionPrivilege;
import com.turing.user.service.FunctionPrivilegeService;
import com.turing.user.service.RoleFunctionPrivilegeService;
import com.turing.user.vo.LoginVO;
import lombok.AllArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @Description 服务类实现
* @Author livin
* @Date 2022-02-22
*/
@Service
public class RoleFunctionPrivilegeServiceImpl extends ServiceImpl<RoleFunctionPrivilegeMapper, RoleFunctionPrivilege> implements RoleFunctionPrivilegeService {

    private static final Log logger = LogFactory.getLog(RoleFunctionPrivilegeServiceImpl.class);

    @Autowired
    private FunctionPrivilegeService functionPrivilegeService;

    @Autowired
    private RoleFunctionPrivilegeMapper roleFunctionPrivilegeMapper;

    @Override
    public List<RoleFunctionPrivilege> getPrivilegeByRole(Integer roleId) {
        LambdaQueryWrapper<RoleFunctionPrivilege> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleFunctionPrivilege::getRoleId, roleId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<LoginVO.Menu> buildMenuTree(Set<RoleFunctionPrivilege> mfpSet) {

        List<RoleFunctionPrivilege> mfps = new ArrayList<>(mfpSet);
        mfps.sort((o1,o2)->o1.getPrivilegeId()-o2.getPrivilegeId());

        List<LoginVO.Menu> menuTree = new ArrayList<>();
        mfps.stream().filter(mfp->mfp.getParentPrivilegeId().equals(0)).forEach(mfp->{
            Integer privilegeId = mfp.getPrivilegeId();
            LoginVO.Menu menuVO = buildMenuVO(privilegeId,0);
            List<LoginVO.Menu> childNodes = new ArrayList<>();
            mfps.stream().filter(m->m.getParentPrivilegeId().equals(privilegeId)).forEach(m->{
                Integer childPrivilegeId = m.getPrivilegeId();
                LoginVO.Menu childMenuVO = buildMenuVO(childPrivilegeId,m.getParentPrivilegeId());
                List<LoginVO.Menu> grandSonNodes = new ArrayList<>();
                mfps.stream().filter(cm->cm.getParentPrivilegeId().equals(childPrivilegeId)).forEach(cm->{
                    Integer grandSonPrivilegeId = cm.getPrivilegeId();
                    LoginVO.Menu grandSonMenuVO = buildMenuVO(grandSonPrivilegeId,cm.getParentPrivilegeId());
                    grandSonNodes.add(grandSonMenuVO);
                });
                childMenuVO.setChildNodes(grandSonNodes);
                childNodes.add(childMenuVO);
            });
            menuVO.setChildNodes(childNodes);
            menuTree.add(menuVO);
        });
        return menuTree;

    }


    private LoginVO.Menu buildMenuVO( Integer privilegeId,Integer parentId ){
        FunctionPrivilege privilege = functionPrivilegeService.getById(privilegeId);
        if(privilege !=null) {
            return LoginVO.Menu.builder().id(privilegeId).privilegeName(privilege.getName()).name(privilege.getEnglishName()).parentId(privilege.getParentPrivilege()).build();
        }
        return new LoginVO.Menu();
    }


}
